<?php
namespace Client\Controller;
use Org\Net\Http;

use Think\Controller;
class PointController extends Controller {
	public function index(){
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$types = array('Ios','Android');
			$geotables = array(
				'Ios'=>'105083',
				'Android'=>'105084'
				);
				$coord_types = array(
					'Ios'=>1,
					'Android'=>2
				);
				$type = $_POST['type'];
				if(in_array_case($type, $types)){
					$http = new Http();
					$post['latitude'] = $_POST['latitude'];
					$post['longitude'] = $_POST['longitude'];
					$post['geotable_id'] = $geotables[$_POST['type']];
					$post['ak'] = C("BAIDU_AK");
					$post['coord_type'] = $coord_types[$_POST['type']];
					$post['title'] = $_POST['name'];
					$post['tags'] = $_POST['belong'];
					$data['post'] = $post;
					$back = $http->fsockopenDownload(C("BAIDU_POI_CREATE"),$data);
//					file_put_contents("Public/Log/log.txt", "back:".$back."\r\n",FILE_APPEND|LOCK_EX);
					$back = json_decode($back);
					if($back->status == 0){
						$json['result'] = true;
						$json['err'] = $back->message;
					}else{
						$json['result'] = false;
						$json['err'] = '查询失败：没有该类型定点';
					}
				}else{
					$json['result'] = false;
					$json['err'] = '查询失败：未知类型';
				}
				$this->ajaxReturn($json);
		}
	}
	//	public function add(){
	//		if(IS_POST){
	//			$types = array('Ios','Android');
	//			$type = $_POST['type'];
	//			if(in_array_case($type, $types)){
	//				$point = D($type);
	//				$data['name'] = $_POST['name'];
	//				$data['latitude'] = $_POST['latitude'];
	//				$data['longitude'] = $_POST['longitude'];
	//				$data['belong'] = $_POST['belong'];
	//				$data = $point->create($data);
	//				$result = $point->add($data);
	//				if($result){
	//					$json['result'] = true;
	//				}else{
	//					$json['result'] = false;
	//					$json['err'] = '添加失败：添加数据库失败';
	//				}
	//			}else{
	//				$json['result'] = false;
	//				$json['err'] = '添加失败：未知类型';
	//			}
	//			$this->ajaxReturn($json);
	//		}
	//	}
	public function query(){
		if(IS_POST){
			$types = array('Ios','Android');
			$type = $_POST['type'];
			if(in_array_case($type, $types)){
				$point = D($type.'Point');
				$result = $point->field("id,name,latitude,longitude,pic_x,pic_y")->select();
				if($result){
					$json['result'] = true;
					$json['points'] = $result;
				}else{
					$json['result'] = false;
					$json['err'] = '查询失败：没有该类型定点';
				}
			}else{
				$json['result'] = false;
				$json['err'] = '查询失败：未知类型';
			}
			$this->ajaxReturn($json);
		}
	}
}